<?php
namespace Home\Controller;
use Think\Auth;

class UserController extends AuthController {
    function index($p = 1){
        $gameObj = M('Users');
        $page_limit = 20;
        $where = array();
            $nickname = I('get.nickname');
            $date = I('get.date');
            if('' !== $date){
                if(false !== strpos($date,'+-+')){
                    $date = explode("+-+",$date);
                }else{
                    $date = explode(" - ",$date);
                }
                $start_date = $date[0];
                $end_date = $date[1];
                if($start_date !== date('Y-m-d') ){
                    $where['date'] = array('between',array(date('Y-m-d H:i:s',strtotime($start_date)),date('Y-m-d H:i:s',strtotime($end_date)+3600*24)));
                }
            }

            if('' !== $nickname){
                $where['nickname'] = $nickname;
            }


        $list = $gameObj
            ->page($p.','.$page_limit)
            ->where($where)
            ->order('id desc')
            ->select();
        $this->assign('list',$list);// 赋值数据集

        $count      = $gameObj->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$page_limit);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        foreach ($list as $key=>$val){
            $share = M('Users')->where(array('path'=>array('like','%,'.$val['id'].',%')))->count();
            $time = M('GameRecord')->where(array('user_id'=>$val['id']))->sum('play_time');
            $time = ($time/3600);
            if($time > 10){
                $time = sprintf("%.1f", $time);
            }else{
                $time = sprintf("%.2f", $time);
            }
            $list[$key]['share'] = $share - 1;
            $list[$key]['time'] = $time;
        }

        $this->assign('list',$list);

        $this->display();
    }
}